<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="招生年度" prop="enrollmentYear">
        <el-input
          v-model="queryParams.enrollmentYear"
          placeholder="请输入招生年度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="招生区县" prop="enrollmentDistrict">
        <el-input
          v-model="queryParams.enrollmentDistrict"
          placeholder="请输入招生区县"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名性质" prop="enrollmentNature">
        <el-select v-model="queryParams.enrollmentNature" placeholder="请选择报名性质" clearable>
          <el-option
            v-for="dict in enrollment_nature"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开网计划" prop="onlinePlan">
        <el-input
          v-model="queryParams.onlinePlan"
          placeholder="请输入开网计划"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名学段" prop="enrollmentLevel">
        <el-input
          v-model="queryParams.enrollmentLevel"
          placeholder="请输入报名学段"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名类型" prop="enrollmentType">
        <el-select v-model="queryParams.enrollmentType" placeholder="请选择报名类型" clearable>
          <el-option
            v-for="dict in enrollment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报名状态" prop="enrollmentStatus">
        <el-select v-model="queryParams.enrollmentStatus" placeholder="请选择报名状态" clearable>
          <el-option
            v-for="dict in enrollment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报名方式" prop="enrollmentMethod">
        <el-select v-model="queryParams.enrollmentMethod" placeholder="请选择报名方式" clearable>
          <el-option
            v-for="dict in enrollment_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报名学校" prop="appliedSchool">
        <el-input
          v-model="queryParams.appliedSchool"
          placeholder="请输入报名学校"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名年级" prop="enrollmentGrade">
        <el-input
          v-model="queryParams.enrollmentGrade"
          placeholder="请输入报名年级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校排号" prop="schoolQueueNumber">
        <el-input
          v-model="queryParams.schoolQueueNumber"
          placeholder="请输入学校排号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名时间" prop="enrollmentTime">
        <el-date-picker clearable
          v-model="queryParams.enrollmentTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择报名时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="录取学校" prop="admittedSchool">
        <el-input
          v-model="queryParams.admittedSchool"
          placeholder="请输入录取学校"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['report:primary_secondary_enrollment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['report:primary_secondary_enrollment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['report:primary_secondary_enrollment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['report:primary_secondary_enrollment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="primary_secondary_enrollmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="招生年度" align="center" prop="enrollmentYear" />
      <el-table-column label="招生区县" align="center" prop="enrollmentDistrict" />
      <el-table-column label="报名性质" align="center" prop="enrollmentNature">
        <template #default="scope">
          <dict-tag :options="enrollment_nature" :value="scope.row.enrollmentNature"/>
        </template>
      </el-table-column>
      <el-table-column label="开网计划" align="center" prop="onlinePlan" />
      <el-table-column label="报名学段" align="center" prop="enrollmentLevel" />
      <el-table-column label="报名类型" align="center" prop="enrollmentType">
        <template #default="scope">
          <dict-tag :options="enrollment_type" :value="scope.row.enrollmentType"/>
        </template>
      </el-table-column>
      <el-table-column label="报名状态" align="center" prop="enrollmentStatus">
        <template #default="scope">
          <dict-tag :options="enrollment_status" :value="scope.row.enrollmentStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="报名方式" align="center" prop="enrollmentMethod">
        <template #default="scope">
          <dict-tag :options="enrollment_method" :value="scope.row.enrollmentMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="报名学校" align="center" prop="appliedSchool" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="报名年级" align="center" prop="enrollmentGrade" />
      <el-table-column label="学校排号" align="center" prop="schoolQueueNumber" />
      <el-table-column label="报名时间" align="center" prop="enrollmentTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.enrollmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="录取学校" align="center" prop="admittedSchool" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['report:primary_secondary_enrollment:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['report:primary_secondary_enrollment:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改中小学报名信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="primary_secondary_enrollmentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="招生年度" prop="enrollmentYear">
          <el-input v-model="form.enrollmentYear" placeholder="请输入招生年度" />
        </el-form-item>
        <el-form-item label="招生区县" prop="enrollmentDistrict">
          <el-input v-model="form.enrollmentDistrict" placeholder="请输入招生区县" />
        </el-form-item>
        <el-form-item label="报名性质" prop="enrollmentNature">
          <el-select v-model="form.enrollmentNature" placeholder="请选择报名性质">
            <el-option
              v-for="dict in enrollment_nature"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开网计划" prop="onlinePlan">
          <el-input v-model="form.onlinePlan" placeholder="请输入开网计划" />
        </el-form-item>
        <el-form-item label="报名学段" prop="enrollmentLevel">
          <el-input v-model="form.enrollmentLevel" placeholder="请输入报名学段" />
        </el-form-item>
        <el-form-item label="报名类型" prop="enrollmentType">
          <el-select v-model="form.enrollmentType" placeholder="请选择报名类型">
            <el-option
              v-for="dict in enrollment_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报名状态" prop="enrollmentStatus">
          <el-select v-model="form.enrollmentStatus" placeholder="请选择报名状态">
            <el-option
              v-for="dict in enrollment_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报名方式" prop="enrollmentMethod">
          <el-select v-model="form.enrollmentMethod" placeholder="请选择报名方式">
            <el-option
              v-for="dict in enrollment_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报名学校" prop="appliedSchool">
          <el-input v-model="form.appliedSchool" placeholder="请输入报名学校" />
        </el-form-item>
        <el-form-item label="班级" prop="className">
          <el-input v-model="form.className" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="报名年级" prop="enrollmentGrade">
          <el-input v-model="form.enrollmentGrade" placeholder="请输入报名年级" />
        </el-form-item>
        <el-form-item label="学校排号" prop="schoolQueueNumber">
          <el-input v-model="form.schoolQueueNumber" placeholder="请输入学校排号" />
        </el-form-item>
        <el-form-item label="报名时间" prop="enrollmentTime">
          <el-date-picker clearable
            v-model="form.enrollmentTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择报名时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="录取学校" prop="admittedSchool">
          <el-input v-model="form.admittedSchool" placeholder="请输入录取学校" />
        </el-form-item>
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Primary_secondary_enrollment">
import { listPrimary_secondary_enrollment, getPrimary_secondary_enrollment, delPrimary_secondary_enrollment, addPrimary_secondary_enrollment, updatePrimary_secondary_enrollment } from "@/api/report/primary_secondary_enrollment"

const { proxy } = getCurrentInstance()
const { enrollment_method, enrollment_nature, enrollment_status, enrollment_type } = proxy.useDict('enrollment_method', 'enrollment_nature', 'enrollment_status', 'enrollment_type')

const primary_secondary_enrollmentList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    enrollmentYear: null,
    enrollmentDistrict: null,
    enrollmentNature: null,
    onlinePlan: null,
    enrollmentLevel: null,
    enrollmentType: null,
    enrollmentStatus: null,
    enrollmentMethod: null,
    appliedSchool: null,
    className: null,
    enrollmentGrade: null,
    schoolQueueNumber: null,
    enrollmentTime: null,
    admittedSchool: null,
    studentName: null
  },
  rules: {
    enrollmentYear: [
      { required: true, message: "招生年度不能为空", trigger: "blur" }
    ],
    enrollmentDistrict: [
      { required: true, message: "招生区县不能为空", trigger: "blur" }
    ],
    enrollmentNature: [
      { required: true, message: "报名性质不能为空", trigger: "change" }
    ],
    onlinePlan: [
      { required: true, message: "开网计划不能为空", trigger: "blur" }
    ],
    enrollmentLevel: [
      { required: true, message: "报名学段不能为空", trigger: "blur" }
    ],
    enrollmentType: [
      { required: true, message: "报名类型不能为空", trigger: "change" }
    ],
    enrollmentStatus: [
      { required: true, message: "报名状态不能为空", trigger: "change" }
    ],
    enrollmentMethod: [
      { required: true, message: "报名方式不能为空", trigger: "change" }
    ],
    appliedSchool: [
      { required: true, message: "报名学校不能为空", trigger: "blur" }
    ],
    className: [
      { required: true, message: "班级不能为空", trigger: "blur" }
    ],
    enrollmentGrade: [
      { required: true, message: "报名年级不能为空", trigger: "blur" }
    ],
    schoolQueueNumber: [
      { required: true, message: "学校排号不能为空", trigger: "blur" }
    ],
    enrollmentTime: [
      { required: true, message: "报名时间不能为空", trigger: "blur" }
    ],
    admittedSchool: [
      { required: true, message: "录取学校不能为空", trigger: "blur" }
    ],
    studentName: [
      { required: true, message: "学生姓名不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询中小学报名信息列表 */
function getList() {
  loading.value = true
  listPrimary_secondary_enrollment(queryParams.value).then(response => {
    primary_secondary_enrollmentList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    enrollmentYear: null,
    enrollmentDistrict: null,
    enrollmentNature: null,
    onlinePlan: null,
    enrollmentLevel: null,
    enrollmentType: null,
    enrollmentStatus: null,
    enrollmentMethod: null,
    appliedSchool: null,
    className: null,
    enrollmentGrade: null,
    schoolQueueNumber: null,
    enrollmentTime: null,
    admittedSchool: null,
    studentName: null
  }
  proxy.resetForm("primary_secondary_enrollmentRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加中小学报名信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPrimary_secondary_enrollment(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改中小学报名信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["primary_secondary_enrollmentRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePrimary_secondary_enrollment(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPrimary_secondary_enrollment(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除中小学报名信息编号为"' + _ids + '"的数据项？').then(function() {
    return delPrimary_secondary_enrollment(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('report/primary_secondary_enrollment/export', {
    ...queryParams.value
  }, `primary_secondary_enrollment_${new Date().getTime()}.xlsx`)
}

getList()
</script>
